﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using MTodo.ViewModel;
using WenslyWuLib.Common;
using WenslyWuLib.Common.DynamicEffect;
using MTodo.Model;
using MTodo.ViewInterface;
using System.Windows.Controls.Primitives;

namespace MTodo
{
    public partial class MainPage : PhoneApplicationPage
    {
        #region Fields
        private TimeGroupsViewModel _groupsViewModel;
        private ListBoxItem _lastSelectedFolderItem=null;
        private NewMemo _newWindow;  
      
        #endregion

        #region Properties

        /// <summary>
        /// Gets Popup Window
        /// </summary>
        public NewMemo NewWindow
        {
            get { return this._newWindow; }
        }

        /// <summary>
        /// Gets add Voew pop
        /// </summary>
        public Popup AddViewPop
        {
            get { return this.addViewPop; }
        }
        #endregion

        #region Methods
        // Constructor
        public MainPage()
        {
            InitializeComponent(); 
            _newWindow =new NewMemo();
            this.Loaded += new RoutedEventHandler(MainPage_Loaded);            
        }
      
        void MainPage_Loaded(object sender, RoutedEventArgs e)
        {
            var obj = typeof(MainPage);
            _groupsViewModel = new TimeGroupsViewModel();
            _groupsViewModel.Title = "My Time Line";
            _groupsViewModel.Page = this;
            this.DataContext = _groupsViewModel;
            //this.TimeGroups.ItemsSource = _groupsViewModel.TimeGroups;
            //this.TimeGroups.DataContext = _groupsViewModel;
            this.TimeGroups.SelectionChanged += new SelectionChangedEventHandler(TimeGroups_SelectionChanged);
            //this.TimeGroups.Tap += new EventHandler<GestureEventArgs>(TimeGroups_Tap);
            this.TimeGroups.Loaded += new RoutedEventHandler(TimeGroups_Loaded);
        }


        /// <summary>
        /// TimeGroup Loaded
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void TimeGroups_Loaded(object sender, RoutedEventArgs e)
        {
            for (int i = 0; i < 3; i++)
            {
                ListBoxItem item = this.TimeGroups.ItemContainerGenerator.ContainerFromIndex(i) as ListBoxItem;
                if (item != null)
                {
                    item.Visibility = Visibility.Visible;
                    List<ListBox> stack = Common.FindAllChildOfType<ListBox>(item);
                    ListBox itemDtl = stack.FirstOrDefault();

                    this.Dispatcher.BeginInvoke(() =>
                    {
                        itemDtl.Visibility = Visibility.Visible;
                        itemDtl.Visibility = Visibility.Collapsed;
                    });
                }
            }
        }

        void TimeGroups_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            #region
            ListBoxItem item = this.TimeGroups.ItemContainerGenerator.ContainerFromIndex((sender as ListBox).SelectedIndex) as ListBoxItem;
            if(item==null)
                return;

            item.Visibility = Visibility.Visible;
            List<ListBox> stack = Common.FindAllChildOfType<ListBox>(item);
            ListBox itemDtl = stack.FirstOrDefault();

            if (_lastSelectedFolderItem != null)
            {
                List<ListBox> lastStack = Common.FindAllChildOfType<ListBox>(_lastSelectedFolderItem);
                var ls = lastStack.FirstOrDefault();
                this.Dispatcher.BeginInvoke(() =>
                {
                    AnimationProvider.FadeOutObject(ls, CollapseItem, itemDtl);
                });
            }

            if (itemDtl != null && _lastSelectedFolderItem == null)
            {
                itemDtl.Visibility = Visibility.Visible;
                this.Dispatcher.BeginInvoke(() =>
                {
                    AnimationProvider.RightToLeftAnimation(itemDtl, this);
                });
            }
            _lastSelectedFolderItem = item;

            #endregion
        }

        private void CollapseItem(Object item)
        {
            ListBox itemDtl = item as ListBox;
            if (itemDtl == null)
                return;

            if (itemDtl != null)
            { itemDtl.Visibility = Visibility.Visible; }
            this.Dispatcher.BeginInvoke(() =>
            {
                AnimationProvider.RightToLeftAnimation(itemDtl, this);
            });
        }

        #endregion

        #region SystemMethods

        protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
        {
            if (addViewPop.IsOpen)
            {
                var newItemView = addViewPop.Child as NewMemo; 
                if (newItemView != null && newItemView.GetNewItemObject()!=null)
                {                    
                    this._groupsViewModel.AddNewItem(newItemView.GetNewItemObject());
                    //this.DataContext = null;
                    //this.DataContext = _groupsViewModel;
                    this.UpdateLayout();
                    //TimeGroups_Loaded(null, null);
                }
                _newWindow.HiddenDialog(() => { addViewPop.IsOpen = false; });
                e.Cancel = true;
            }           
        }

        #endregion  
    }
}